package cn.youyeku.zsxh.ui.activity.tabAct;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.youyeku.zsxh.R;
import cn.youyeku.zsxh.Utils.NullUtils;
import cn.youyeku.zsxh.api.XhApi;
import cn.youyeku.zsxh.app;
import cn.youyeku.zsxh.ui.activity.ABS.BaseActivity;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by youye on 17-4-4.
 */

public class ActMutualHelpPost extends BaseActivity {

    @BindView(R.id.ed_send_help_message_address)
    EditText edSendHelpMessageAddress;
    @BindView(R.id.ed_send_help_message_reward)
    EditText edSendHelpMessageReward;
    @BindView(R.id.ed_send_help_message_text)
    EditText edSendHelpMessageText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_act_mutual_help_send);
        ButterKnife.bind(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        menu.add(Menu.NONE, Menu.FIRST, Menu.NONE, "发布")
                .setEnabled(true)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);


        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case Menu.FIRST:
                post_help();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void post_help() {
        String address = edSendHelpMessageAddress.getText().toString();
        String reward = edSendHelpMessageReward.getText().toString();
        String text = edSendHelpMessageText.getText().toString();

        if(NullUtils.isArrayNotNull(new String[]{address,reward,text})){
            XhApi.post_helps(text, address, reward, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    try {
                        JSONObject object = new JSONObject(response.body().string());
                        app.backgroundThreadShortToast(object.getString("message"));
                        if(object.getInt("code")==200){
                            finish();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

    }
}
